package top.novel.strategy;

import cn.hutool.core.util.StrUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import top.novel.persistence.PersistenceData;

import javax.swing.*;

/**
 * @author xuhainan
 * @date 2022/8/12 09:05
 * @region hefei
 */
public class Miaojiang8Strategy implements IStrategy {

    public static final String PREFIX = "https://m.miaojiang8.net";

    @Override
    public String getPrefix() {
        return PREFIX;
    }

    @Override
    public void analyze(JTextPane content, PersistenceData persistenceData) {
        content.setContentType("text/html");
        String recallContent = this.recallContent(persistenceData.getUrl());
        Document document = Jsoup.parse(recallContent);
        Element title = document.getElementById("nr_title");
        Element element = document.getElementById("nr1");
        String result = StrUtil.concat(true, "<h1>", title.html(), "</h1>", element.html());
        content.setText(result);
        content.setCaretPosition(0);
        Element pre = document.getElementById("pb_prev");
        String preHref = pre.attr("href");
        persistenceData.setPreUrl(PREFIX + preHref);

        Element next = document.getElementById("pb_next");
        String nextHref = next.attr("href");
        persistenceData.setNextUrl(PREFIX + nextHref);
    }
}
